package com.feige.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.feige.pojo.Role;
import com.feige.pojo.User;
import com.feige.pojo.dto.RoleFormDto;
import com.feige.pojo.dto.RoleQueryDto;
import com.feige.pojo.vo.RoleVo;
import com.feige.result.AjaxResult;
import com.feige.service.RoleService;
import com.feige.service.UserService;
import com.feige.utils.common.BeanUtils;
import com.feige.utils.common.Admin;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 角色表 前端控制器
 * </p>
 *
 * @author feige
 * @since 2020-07-20
 */
@Api(tags = "角色类")
@RestController
@RequestMapping("/admin/role")
public class RoleController {

    @Resource
    private RoleService roleService;

    @Resource
    private UserService userService;

    @PreAuthorize("@ss.hasPerm('user:role:list')")
    @ApiOperation(value = "获取角色分页列表")
    @PostMapping("/list")
    public AjaxResult index(@RequestBody RoleQueryDto roleQueryDto){
        IPage<Role> pageParam = roleService.queryListByRoleQueryDto(roleQueryDto);
        List<RoleVo> roleVos = BeanUtils.convertList2List(pageParam.getRecords(), RoleVo.class);
        AjaxResult success = AjaxResult.success(roleVos);
        success.put("total",pageParam.getTotal());
        return success;
    }

    @PreAuthorize("@ss.hasPerm('user:role:list')")
    @ApiOperation(value = "获取所有角色")
    @GetMapping("/get-all")
    public AjaxResult getAll() {
        List<Role> list = roleService.list(null);
        List<Role> roleList = list.stream().filter(role -> !Admin.isSuperAdminRole(role.getId())).collect(Collectors.toList());
        return AjaxResult.success(roleList);
    }


    @PreAuthorize("@ss.hasPerm('user:role:list')")
    @ApiOperation(value = "获取角色")
    @GetMapping("/get/{id}")
    public AjaxResult get(@PathVariable Long id) {
        Role role = roleService.getById(id);
        return AjaxResult.success(role);
    }

    @PreAuthorize("@ss.hasPerm('user:role:add')")
    @ApiOperation(value = "新增角色和权限")
    @PostMapping("/save")
    public AjaxResult save(@Validated @RequestBody RoleFormDto roleFormDto) {
        boolean save = roleService.saveRole(roleFormDto);
        if (save){
            return AjaxResult.success();
        }else {
            return AjaxResult.error();
        }
    }

    @PreAuthorize("@ss.hasPerm('user:role:edit')")
    @ApiOperation(value = "修改角色和权限")
    @PutMapping("/update")
    public AjaxResult updateById(@Validated @RequestBody RoleFormDto roleFormDto) {
        boolean update = roleService.updateRole(roleFormDto);
        if (update){
            return AjaxResult.success();
        }else {
            return AjaxResult.error();
        }
    }

    @PreAuthorize("@ss.hasPerm('user:role:edit')")
    @ApiOperation(value = "修改角色的状态")
    @PutMapping("/update-status")
    public AjaxResult update(@RequestBody Role role) {
        boolean update = roleService.updateById(role);
        if (update){
            return AjaxResult.success();
        }else {
            return AjaxResult.error();
        }
    }

    @PreAuthorize("@ss.hasPerm('user:role:delete')")
    @ApiOperation(value = "删除角色")
    @DeleteMapping("/remove/{id}")
    public AjaxResult remove(@PathVariable Long id) {
        boolean remove = roleService.removeById(id);
        if (remove){
            return AjaxResult.success();
        }else {
            return AjaxResult.error();
        }
    }

    @PreAuthorize("@ss.hasPerm('user:role:delete')")
    @ApiOperation(value = "根据id列表删除角色")
    @DeleteMapping("/batchRemove")
    public AjaxResult batchRemove(@RequestBody List<Long> idList) {
        boolean remove = roleService.removeByIds(idList);
        if (remove){
            return AjaxResult.success();
        }else {
            return AjaxResult.error();
        }
    }
    @PreAuthorize("@ss.hasPerm('user:role:list')")
    @ApiOperation(value = "根据用户获取角色数据")
    @GetMapping("/get-role/{userId}")
    public AjaxResult toAssign(@PathVariable Long userId) {
        User user = userService.getById(userId);
        List<Role> list = roleService.list(null);
        List<Role> roleList = list.stream().filter(role -> !Admin.isSuperAdminRole(role.getId())).collect(Collectors.toList());
        List<Role> roles = Admin.isAdmin(user.getNum()) ? list : roleList;
        List<Long> roleIds = roleService.listRoleIdByUserId(userId);
        AjaxResult success = AjaxResult.success();
        success.put("user",user);
        success.put("roles",roles);
        success.put("roleIds",roleIds);
        return success;
    }
}

